package SMS.service;

import SMS.mapper.StudentsMapper;
import SMS.pojo.Students;
import SMS.vo.IStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王天宇
 */
@Service
public class StudentService {
//    引入StudentsMapper
    @Autowired
    private StudentsMapper studentsMapper; // 注入 StudentMapper

    public List<IStudent> getStudents(int currentPage, int pageSize) {
        // 计算查询的起始索引
        int offset = (currentPage - 1) * pageSize;

        // 调用 Mapper 方法进行查询
        return studentsMapper.listStudents(offset, pageSize);
    }
    public int getTotalCount() {
        return studentsMapper.getTotalCount();
    }

    public void updateStudent(Students student) {
        System.out.println("service:"+student);
        studentsMapper.updateByid(student.getId(),student.getStuName(),student.getStuNo(),student.getPhoto(),student.getGender(),student.getPhone(),student.getEmail(),student.getAddress(),student.getClassNo(),student.getSelectedCorseNo(),student.getRemark());
//        studentsMapper.deleteByPrimaryKey(student.getId());
//        studentsMapper.updateByPrimaryKeySelective(student);
    }

    public void deleteStudent(Long id) {
        studentsMapper.deleteByPrimaryKey(id);
    }

    public void addStudent(Students student) {
        studentsMapper.insert(student);
    }

    public int countByStuNo(String no) {
        return studentsMapper.countByStuNo(no);
    }

    public int countByStuName(String name) {
        return studentsMapper.countByStuName(name);
    }

    public List<IStudent> searchStudentByName(String stuName, int currentPage, int pageSize) {
        // 计算查询的起始索引
        int offset = (currentPage - 1) * pageSize;
        return studentsMapper.searchStudentByName(stuName,offset,pageSize);
    }

    public List<IStudent> searchStudentByNo(String req, int currentPage, int pageSize) {
        // 计算查询的起始索引
        int offset = (currentPage - 1) * pageSize;
        return studentsMapper.searchStudentByNo(req,offset,pageSize);
    }
}
